package com.zmn.oms.business.impl.remark;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.business.interfaces.remark.OrderRemarkBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.model.dto.remark.OrderRemarkDTO;
import com.zmn.oms.model.dto.remark.OrderRemarkDetailDTO;
import com.zmn.oms.model.dto.work.modify.must.ZsCancelDTO;
import com.zmn.oms.model.entity.remark.OrderRemark;
import com.zmn.oms.model.entity.remark.OrderRemarkDetail;
import com.zmn.oms.model.vo.remark.OrderRemarkDetailVO;
import com.zmn.oms.model.vo.remark.OrderRemarkVO;
import com.zmn.oms.services.interfaces.remark.OrderRemarkDetailService;
import com.zmn.oms.services.interfaces.remark.OrderRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 类描述：
 *
 * @author lujia
 * @date 2018/12/05 19:20
 */
@Service
public class OrderRemarkBServiceImpl implements OrderRemarkBService {

    @Autowired
    private OrderRemarkService orderRemarkService;
    @Autowired
    private OrderRemarkDetailService orderRemarkDetailService;

    @Override
    public OrderRemarkVO findOrderRemarkVO(Long orderId, Long workId, Integer type) {

        OrderRemarkVO orderRemarkVO = null;

        OrderRemark orderRemark = orderRemarkService.findByOrderIdAndType(orderId, workId, type);
        if (orderRemark == null) {
            return null;
        }

        List<OrderRemarkDetailVO> orderRemarkDetailVOList = Lists.newArrayList();
        List<OrderRemarkDetail> orderRemarkDetailList =  orderRemarkDetailService
                .listOrderRemarkDetailByRemarkId(orderId, workId, orderRemark.getRemarkId());
        orderRemarkDetailList.forEach((detail -> {
            orderRemarkDetailVOList.add(new OrderRemarkDetailVO(detail.getMapId(), detail.getMapName(),detail.getCategory()));
        }));

        orderRemarkVO = new OrderRemarkVO();
        orderRemarkVO.setContent(orderRemark.getContent());
        orderRemarkVO.setDetailVOList(orderRemarkDetailVOList);

        return orderRemarkVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertOrderRemark(OrderRemarkDTO dto, int orderRemarkType) {

        // 删除之前备注 先删后增
        OrderRemark orderRemark = new OrderRemark();
        orderRemark.setOrderId(dto.getOrderId());
        orderRemark.setWorkId(dto.getWorkId());
        orderRemark.setType(orderRemarkType);
        orderRemarkService.deleteOrderRemarkByParams(orderRemark);

        // 删除之前的备注详情
        OrderRemarkDetail orderRemarkDetail = new OrderRemarkDetail();
        orderRemarkDetail.setOrderId(dto.getOrderId());
        orderRemarkDetail.setWorkId(dto.getWorkId());
        orderRemarkDetail.setType(orderRemarkType);
        orderRemarkDetailService.deleteOrderRemarkDetailByParams(orderRemarkDetail);

        // 保存备注
        orderRemark.setContent(dto.getContent());
        orderRemark.setOperateUserId(dto.getOperatorId());
        orderRemark.setOperateUserType(dto.getOperatorType());
        orderRemarkService.insertOrderRemark(orderRemark);

        // 备注Id
        // 保存备注详情
        Long remarkId = orderRemark.getRemarkId();
        List<OrderRemarkDetailDTO> detailDTOList = dto.getDetailDTOList();

        if (detailDTOList != null && !detailDTOList.isEmpty()) {
            List<OrderRemarkDetail> detailList = Lists.newArrayList();
            detailDTOList.forEach(detailDTO -> {
                OrderRemarkDetail detail = new OrderRemarkDetail();
                detail.setRemarkId(remarkId);
                detail.setOrderId(dto.getOrderId());
                detail.setWorkId(dto.getWorkId());
                detail.setType(orderRemarkType);
                detail.setMapId(detailDTO.getMapId());
                detail.setMapName(detailDTO.getMapName());
                detail.setMapValue(detailDTO.getMapValue());
                detail.setCategory(Objects.nonNull(detailDTO.getCategory()) ? detailDTO.getCategory() : GlobalConsts.NONE);
                detail.setCreater(dto.getOperator());
                detailList.add(detail);
            });

            orderRemarkDetailService.insertOrderRemarkList(detailList);
        }
        // Proxy接口调用取消
        else if (dto instanceof ZsCancelDTO) {
            ZsCancelDTO cancelDTO = (ZsCancelDTO)dto;
            // 审核失败备注
            if (Objects.equals(cancelDTO.getReviewStatus(), OrderConsts.REVIEW_STATUS_FAIL)) {
                // 暂不处理
            }
            // Proxy接口调用取消，超时未支付导致系统自动取消，记一条详情
            else {
                // 超时未支付取消
                if (Objects.equals(GlobalConsts.YES, cancelDTO.getPayTimeout())) {
                    List<OrderRemarkDetail> detailList = Lists.newArrayList();
                    OrderRemarkDetail detail = new OrderRemarkDetail();
                    detail.setRemarkId(remarkId);
                    detail.setOrderId(dto.getOrderId());
                    detail.setWorkId(dto.getWorkId());
                    detail.setType(orderRemarkType);
                    detail.setCreater(dto.getOperator());
                    detail.setMapId(OrderConsts.CANCEL_PAY_TIMEOUT_ID);
                    detail.setMapName("用户超时未支付");
                    detail.setMapValue(null);
                    detail.setCategory(GlobalConsts.NONE);
                    detailList.add(detail);

                    orderRemarkDetailService.insertOrderRemarkList(detailList);
                }
            }
        }
    }
}
